import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:kq_flutter_core_widget/utils/kq_screen_util.dart';
import 'package:get/get.dart';
import 'package:kq_flutter_core_widget/widgets/button/kq_ink_well.dart';

import '../../../resources/kq_theme_colors.dart';
import '../../../resources/l10n/kq_intl.dart';
import '../../../theme/kq_theme_manager.dart';
import '../../divider/kq_divider.dart';

/// 底部弹出-列表菜单
///
/// @author 周卓
///
class KqActionListBottomSheet extends StatelessWidget {
  /// 数据源
  final List<KqActionListData> items;

  /// 标题，不传则不显示
  final String? title;

  /// 是否显示取消按钮，默认true
  final bool showCancel;

  /// 选项文字对齐方式，默认居中
  final Alignment? alignment;

  /// 回调
  final Function(int index, KqActionListData item)? callback;

  /// 点击取消按钮回调
  final VoidCallback? onCancel;

  const KqActionListBottomSheet(
      {Key? key,
      required this.items,
      this.title,
      this.showCancel = false,
      this.callback,
      this.alignment,
      this.onCancel})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    final List<Widget> children = <Widget>[];

    // 小诸葛规范，最多显示6个半，多余滚动
    var maxHeight = 6.5 * 48.r;

    for (int i = 0; i < items.length; i++) {
      var item = items[i];
      children.add(_buildListItem(context, i, item));
    }
    Widget dialogChild = Column(
      mainAxisSize: MainAxisSize.min,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: children,
    );

    dialogChild = DecoratedBox(
      decoration: BoxDecoration(
          color: KqThemeColors.bgWhite,
          borderRadius: BorderRadius.all(Radius.circular(4.r))),
      child: dialogChild,
    );

    dialogChild = Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.r),
      child: dialogChild,
    );

    dialogChild = SingleChildScrollView(child: dialogChild);
    dialogChild = ScrollConfiguration(
      behavior: const ScrollBehavior().copyWith(overscroll: false),
      child: dialogChild,
    );

    var bottomHeight = KqScreenUtil().bottomBarHeight;
    if (bottomHeight <= 32.r) {
      bottomHeight = 32.r;
    }

    var radius = Radius.circular(
        KqThemeManager.instance.getConfig().dialogConfig.radius ?? 8.r);

    return UnconstrainedBox(
        child: SizedBox(
      width: context.width,
      child: Material(
          shape: RoundedRectangleBorder(
              borderRadius:
                  BorderRadius.only(topLeft: radius, topRight: radius)),
          color: KqThemeManager.instance.getConfig().dialogConfig.bottomSheetBgColor ??
              KqThemeColors.bgFA,
          child: Column(
            children: [
              _buildTitleWidget(),
              LimitedBox(
                maxHeight: maxHeight,
                child: ClipRRect(
                  borderRadius:
                      BorderRadius.only(topLeft: radius, topRight: radius),
                  child: dialogChild,
                ),
              ),
              _buildCancelWidget(context),
              SizedBox(height: bottomHeight)
            ],
          )),
    ));
  }

  Widget _buildListItem(
      BuildContext context, int index, KqActionListData item) {
    String title = item.item;
    Widget result;

    result = Text(
      title,
      maxLines: 1,
      overflow: TextOverflow.ellipsis,
      style: TextStyle(
          fontSize: 16.sp, color: item.textColor ?? KqThemeColors.text26),
    );

    if (item.icon != null) {
      result = Row(
        mainAxisSize: MainAxisSize.min,
        children: [
          Image.asset(
            item.icon!,
            width: 14.r,
            height: 14.r,
            package: item.iconPackage,
          ),
          SizedBox(width: 13.r),
          Expanded(child: result)
        ],
      );
    }

    result = Container(
      height: 48.r,
      alignment: alignment ?? Alignment.center,
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 16.r),
      child: result,
    );

    result = KqInkWell(
      onTap: () {
        Navigator.pop(context);
        if (callback != null) {
          callback!(index, item);
        }
      },
      child: result,
    );

    if (index > 0) {
      result = Column(
        children: [
          Padding(
            padding: EdgeInsets.only(left: 40.r),
            child: const KqDivider(),
          ),
          result
        ],
      );
    }
    return result;
  }

  Widget _buildTitleWidget() {
    if (title == null) {
      return SizedBox(height: 16.r);
    }

    return Container(
      height: 40.r,
      alignment: Alignment.center,
      width: double.infinity,
      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 20.r),
      child: Text(
        title!,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 12.sp, color: KqThemeColors.text8C),
      ),
    );
  }

  Widget _buildCancelWidget(BuildContext context) {
    if (!showCancel) {
      return const SizedBox();
    }

    Widget result = Container(
      height: 48.r,
      alignment: Alignment.center,
      width: double.infinity,
      decoration: BoxDecoration(
          color: KqThemeColors.bgWhite,
          borderRadius: BorderRadius.all(Radius.circular(4.r))),
      padding: EdgeInsets.symmetric(vertical: 0, horizontal: 20.r),
      child: Text(
        KqIntl.currentResource.cancel,
        maxLines: 1,
        overflow: TextOverflow.ellipsis,
        style: TextStyle(fontSize: 16.sp, color: KqThemeColors.text26),
      ),
    );

    result = KqInkWell(
      onTap: () {
        Navigator.pop(context);
        onCancel?.call();
      },
      child: result,
    );

    result = Padding(
      padding: EdgeInsets.symmetric(horizontal: 16.r),
      child: result,
    );
    result = Column(
      children: [SizedBox(height: 8.r), result],
    );
    return result;
  }
}

class KqActionListData {
  /// 选项标题
  final String item;

  /// 选项id，非必填
  final String? itemId;

  /// 选项文字颜色
  final Color? textColor;

  final String? icon;

  final String? iconPackage;

  /// 自定义内容
  final dynamic extra;

  KqActionListData({
    required this.item,
    this.itemId,
    this.textColor,
    this.icon,
    this.iconPackage,
    this.extra,
  });
}
